﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using web.core.ViewModels;
using web.core.Models;
using web.component;

namespace web.core.Controllers
{
    public class admEmailTempController : baseController
    {
        //------------------------------------
        //email_temp_list PAGE
        //------------------------------------
        #region email_temp_list PAGE
        public ActionResult email_temp_list()
        {
            bool permission = false;
            InitPage(ref permission);
            ViewBag.permission = permission;
            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult ctr_ct_list(int? pageindex)
        {
           // int? languageId = languageModels.GetLanguageIDbyUrl(Request.Url.PathAndQuery);
            return ctr_ct_list(pageindex, admin_const.PAGE_LIST_SIZE );
        }

        [ChildActionOnly]
        [HttpPost]
        public ActionResult ctr_ct_list(int? pageindex, int? pagesize)
        {
            var lstdelete = Request.Form["is_delete"];
            if (!string.IsNullOrEmpty(lstdelete)) {
                var arr = lstdelete.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach(var iterator in arr){
                    if (iterator != "false")
                        emailTemplateModels.deleteItem(Convert.ToInt32(iterator));
                }
            } 
            var page_view = new ViewModels.EmailTemplatePageListViewModels()
            { 
                //list_group = accountModels.getListUserGroup(),
                list_pagesize = GetSizePagingPublic(pagesize ?? admin_const.PAGE_LIST_SIZE),
                pagesize = pagesize ?? admin_const.PAGE_LIST_SIZE
            };
            TryUpdateModel(page_view);

            page_view.page_list_template = Models.emailTemplateModels.email_template_plist(pageindex ?? 1, pagesize ?? admin_const.PAGE_LIST_SIZE);
            return PartialView("../Shared/email_temp/list_temp", page_view);
        }

        public ActionResult updateStatus(string id)
        {
            int pid = int.Parse(id.Split(',')[0]);
            var sid = int.Parse(id.Split(',')[1]);
            byte isv = Convert.ToByte(sid);
            emailTemplateModels.updateStatus(pid, isv);
            string outStr = "" + sid.ToString();
            return Json(outStr);
        }
        #endregion

        //------------------------------------
        //email_temp_edit PAGE
        //------------------------------------
        #region email_temp_edit PAGE
        public ActionResult email_temp_edit()
        {
            bool permission = false;
            InitPage(ref permission);
            ViewBag.permission = permission;
            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult ctr_ct_edit(int? id)
        {
            int itemid = id ?? 0;
            if (itemid == 0)
                ViewBag.isAdd = true;
            else
                ViewBag.isAdd = false;

            ViewModels.EmailTemplateViewModels item_view; 
            var it = emailTemplateModels.getByID(itemid);
            if (it != null)
            {
                 
                item_view = new EmailTemplateViewModels()
                {
                    email_id = it.email_id,
                    email_name = it.email_name,
                    email_subject = it.email_subject,
                    email_content = it.email_content,
                    email_order = it.email_order??1,
                   // modified = it.modified,
                    email_status = it.email_status??1,
                    modified_by = it.modified_by ??0
                };
            }
            else
                item_view = new EmailTemplateViewModels()
                {
                    email_status = admin_const.DEFAULT_ITEM_STATUS, 
                    email_order = admin_const.DEFAULT_ORDER_DISPLAY 
                };
             
            item_view.select_list_status = new List<SelectListItem>() {  
                    new SelectListItem{Selected=item_view.email_status == 1?true:false, Text = "Kích hoạt", Value = "1" },
                    new SelectListItem{Selected=item_view.email_status == 0?true:false, Text = "Vô hiệu hóa", Value = "0" }
            };

            return PartialView("../Shared/email_temp/edit_temp", item_view);
        }

        [ChildActionOnly]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult ctr_ct_edit(FormCollection collection)
        {
            var item_view = new ViewModels.EmailTemplateViewModels();
            TryUpdateModel(item_view);

            if (string.IsNullOrEmpty(item_view.email_name) || string.IsNullOrEmpty(item_view.email_subject))
            {
                ViewBag.result_message = "Tên & tiêu đề không để trống.";
                ViewBag.result = false;
                return PartialView("../Shared/email_temp/edit_temp", item_view);
            }
            bool isdel = item_view.email_id != 0 ? true : false;
            var item = new data.entities.email_template()
            {
                email_id = item_view.email_id,
                email_name = item_view.email_name,
                email_subject = item_view.email_subject,
                email_content = item_view.email_content,
                email_order = item_view.email_order,
                modified = Udate_time.GetCurrentDateTime(),
                email_status = item_view.email_status,
                modified_by = int.Parse(Session["user_id"].ToString())
            };

            int ret_id = emailTemplateModels.email_template_update(item);

            if (ret_id > 0)
            {
                ViewBag.result_message = admin_const.UPDATE_OK;
                ViewBag.redirect = "/vi/admEmailTemp/email_temp_list";
                return PartialView("../Shared/control_redirect");
            }
            else
            {
                ViewBag.result_message = admin_const.UPDATE_FAIL;
                ViewBag.result = false;
                return PartialView("../Shared/email_temp/edit_temp", item);
            }
        }
        #endregion

        //------------------------------------
        //email_temp_edit PAGE
        //------------------------------------
        #region email_temp_preview PAGE
        public ActionResult email_temp_preview( int? id)
        {
            int itemid = id ?? 0; 

            ViewModels.EmailTemplateViewModels item_view; 
            var it = emailTemplateModels.getByID(itemid);
            if (it != null)
            { 
                item_view = new EmailTemplateViewModels()
                {
                    email_id = it.email_id,
                    email_name = it.email_name,
                    email_subject = it.email_subject,
                    email_content = it.email_content,
                    email_order = it.email_order ?? 1,
                    // modified = it.modified,
                    email_status = it.email_status ?? 1,
                    modified_by = it.modified_by ?? 0
                };
            }
            else
                item_view = new EmailTemplateViewModels();

            return View(item_view);
        }
        #endregion
    }
}